﻿/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
function GetXmlHttp() {
    var oXmlHttp;
    if (typeof XMLHttpRequest != "undefined") {
        oXmlHttp = new XMLHttpRequest();
        return oXmlHttp;
    }
    else
        if (window.ActiveXObject) {
        var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
        for (var i = 0; i < aVersions.length; i++) {
            try {
                oXmlHttp = new ActiveXObject(aVersions[i]);
                return oXmlHttp;
            }
            catch (oError) {
                //do nothing
            }
        }
    }

    throw new Error("XMLHttp could not be created");
}
////function GetXmlHttp() {	
////	var xmlhttp = false;
////	if (window.XMLHttpRequest)
////	{
////		xmlhttp = new XMLHttpRequest();
////  }
////	else if (window.ActiveXObject)// code for IE
////	{
////		try 
////		{
////			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
////		} 
////		catch (e) 
////		{
////			try 
////			{
////				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
////			} catch (E) {
////				xmlhttp=false;
////			}
////		}
////	}
////	return xmlhttp;
////}


/**
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
var data = response.split(';');
document.getElementById(d1).innerHTML = data[0];
document.getElementById(d2).innerHTML = data[1];
document.getElementById(d3).innerHTML = data[2];
}
</code>
</example>
*/
function ajaxResponse(url, callbackFunction, params) {
    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
                if (xmlhttp.status == 200) {
                    var response = xmlhttp.responseText;
                    var functionToCall = callbackFunction + '(response,' + params + ')';
                    if (debug) {
                        alert(response);
                        alert(functionToCall);
                    }
                    eval(functionToCall);
                } else if (debug) {
                    document.write(xmlhttp.responseText);
                }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
//-------------------------------------------------
function getXML(url, callbackFunction, params) {

    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
                if (xmlhttp.status == 200) {
                    var response = xmlhttp.responseXML;
                    var functionToCall = callbackFunction + '(response,' + params + ')';
                    if (debug) {
                        alert(response);
                        alert(functionToCall);
                    }
                    eval(functionToCall);
                } else if (debug) {
                    document.write(xmlhttp.response);
                }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}

//--------------------------------------------------

/**
///<summary>
///Sets the innerHTML property of obj_id with the response from the passed url./
///</summary>
///<param name="url">The url to make the request to get the response data.</param>
///<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function submitAjax(url, data, callbackFunction, params) {
    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
                if (xmlhttp.status == 200) {
                    var response = xmlhttp.responseText;
                    var functionToCall = callbackFunction + '(response,' + params + ')';
                    //  if(debug){
                    //         alert(response);
                    //            alert (functionToCall);
                    //       }
                    eval(functionToCall);
                }

            }
        }

        xmlhttp.open('POST', url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", data.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.send(data);

    }
}

function SetInnerHTMLFromAjaxResponse(url, obj_id) {
    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
                if (xmlhttp.status == 200) {
                    if (debug) {
                        alert(xmlhttp.responseText);
                    }
                    if (typeof obj_id == 'object') {
                        obj_id.innerHTML = xmlhttp.responseText;
                    } else {
                        document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
                    }
                } else if (debug) {
                    document.Write(xmlhttp.responseText);
                }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}
function readySetValueCombo(url, act, cat, control, ID, Value) {

    getXML(url + "?act=" + act + "&cat=" + cat + "&rand=" + Math.random(), 'setValue', '\'' + control + ' \',\'' + ID + '\',\'' + Value + '\'');
}
function setValue(e, control, ID, Value) {


    var id = e.getElementsByTagName(ID);
    var value = e.getElementsByTagName(Value);
    //	var control=e.getElementsByTagName(control);
    control = control.replace(" ", "")
    var node = document.getElementById(control);
    node.options.length = 0;
    if (id.length > 0) {
        for (var i = 0; i < id.length; i++) {
            node.options[i] = new Option(value.item(i).text, id.item(i).text, true, true);
        }
    }
}

function ResetValuePage(id, page) {
    var combo = document.getElementById(id);
    combo.options.length = 0;
    var num = parseInt(page);
    for (var i = 0; i < num; i++) {
        combo.options[i] = new Option(i + 1, i + 1, false, false);

    }

}
function NewWindow(url, path) {
    var w = 500;
    var h = 500;
    var x = (screen.width - w) / 2;
    var y = (screen.height - h) / 2;
    var chrome = "width=" + w + ",height=" + h + ",left=" + x + ",top=" + y + ",scrollbars=yes";
    var upload = open(url, "upload", chrome);

}

function addRecord(url, Control) {
    var s = Control.split("|");
    var result = "";
    for (var i = 0; i < s.length; i++) {
        result += s[i] + "=" + document.getElementById(s[i]).value + "&";
    }
    //alert(result);
    submitAjax(url + "&rand=" + Math.random(), result, "message");
}
function updateRecord(url, Control) {
    var s = Control.split("|");
    var result = "";
    for (var i = 0; i < s.length; i++) {
        result += s[i] + "=" + document.getElementById(s[i]).value + "&";
    }
    //alert(result);
    submitAjax(url + "&rand=" + Math.random(), result, "message");
    //alert(result);
}
function delRecord(url, data) {
    var str = "";

    for (var i = 0; i < data.length; i++) {
        if (data[i].checked) {
            str += data[i].value + "|";
        }
    }
    //alert(str);
    ajaxResponse(url + "&code=" + str, "message");
}
